package com.pktechan.api.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pktechan.api.entity.CommissionBalance;
import com.pktechan.api.entity.CommissionGenerateFlow;
import com.pktechan.api.entity.CommissionWithdrawFlow;
import com.pktechan.api.exception.CustomException;
import com.pktechan.api.service.CommissionBalanceService;
import com.pktechan.api.service.CommissionGenerateFlowService;
import com.pktechan.api.service.CommissionWithdrawFlowService;
import com.pktechan.api.utils.HtmlUtil;
import com.pktechan.api.vo.CommissionGenerateFlowVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @program: cloudfloating
 * @description: 佣金相关功能
 * @author: qx.zhang
 * @create: 2021-03-05 11:27
 **/
@RestController
@RequestMapping("commissionBalanceController")
public class CommissionBalanceController {

    private static final Logger logger = LoggerFactory.getLogger(CommissionBalanceController.class);

    @Autowired
    private CommissionGenerateFlowService commissionGenerateFlowService;
    @Autowired
    private CommissionWithdrawFlowService commissionWithdrawFlowService;
    @Autowired
    private CommissionBalanceService commissionBalanceService;

    /**
     * 佣金生成明细列表
     * @param commissionGenerateFlow
     * @param response
     */
    @RequestMapping(value = "/getCommissionGenerateDetailsListByCustId",method = RequestMethod.POST)
    public void getCommissionGenerateDetailsListByCustId(@RequestBody CommissionGenerateFlow commissionGenerateFlow,HttpServletResponse response){
        try{
            logger.info("根据custId="+commissionGenerateFlow.getCustId()+"查询对应佣金生成明细列表");
            PageHelper.startPage(commissionGenerateFlow.getPageNo(), commissionGenerateFlow.getPageSize());
            List<CommissionGenerateFlowVo> list = commissionGenerateFlowService.getCommissionGenerateDetailsListByCustId(commissionGenerateFlow.getCustId());
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response,pageInfo);
        }catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        }catch (Exception e){
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response,e.getMessage());
        }
    }

    /**
     * 酒钱变动流水查询
     * @param commissionWithdrawFlow
     * @param response
     */
    @RequestMapping(value = "/getCommissionWithdrawDetailsListByCustId",method = RequestMethod.POST)
    public void getCommissionWithdrawDetailsListByCustId(@RequestBody CommissionWithdrawFlow commissionWithdrawFlow,HttpServletResponse response){
        try{
            logger.info("根据custId="+commissionWithdrawFlow.getCommissionBalanceId()+"查询对应佣金支付明细列表");
            PageHelper.startPage(commissionWithdrawFlow.getPageNo(), commissionWithdrawFlow.getPageSize());
            List<CommissionWithdrawFlow> list = commissionWithdrawFlowService.getCommissionWithdrawDetailsListByCustId(commissionWithdrawFlow.getCustId());
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response,pageInfo);
        }catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        }catch (Exception e){
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response,e.getMessage());
        }
    }

    /**
     * 根据Id查询佣金持仓
     *
     * @param balanceId
     * @param response
     */
    @RequestMapping(value = "/getCommissionBalanceById", method = RequestMethod.POST)
    public void getCommissionBalanceById(@RequestParam Integer balanceId, HttpServletResponse response) {
        try {
            logger.info("根据Id=" + balanceId + "查询对应佣金持仓信息");
            CommissionBalance commissionBalance = commissionBalanceService.getById(balanceId);
            HtmlUtil.writerSuccessJson(response, commissionBalance);
        }catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 佣金提现
     *
     * @param commissionWithdrawFlow
     * @param response
     */
    @RequestMapping(value = "/commissionWithdraw", method = RequestMethod.POST)
    public void commissionWithdraw(@RequestBody CommissionWithdrawFlow commissionWithdrawFlow, HttpServletResponse response) {
        try {
            logger.info("佣金提现");
            commissionWithdrawFlowService.commissionWithdraw(commissionWithdrawFlow);
            HtmlUtil.writerSuccessJson(response);
        }catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据CustId查询佣金持仓
     *
     * @param custId
     * @param response
     */
    @RequestMapping(value = "/getCommissionBalanceByCustId", method = RequestMethod.POST)
    public void getCommissionBalanceByCustId(@RequestParam Integer custId, HttpServletResponse response) {
        try {
            logger.info("根据custId=" + custId + "查询对应佣金持仓信息");
            CommissionBalance commissionBalance = commissionBalanceService.getByCustId(custId);
            HtmlUtil.writerSuccessJson(response, commissionBalance);
        }catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }
}
